#include "Card.h"
#include <iostream>
#include <stdlib.h>
#include <ctype.h>
#include <string>
#include <stdio.h>
using std::ostream;
using std::istream;
using std::cout;
using std::string;

#define RESET		0
#define BRIGHT 		1
#define DIM		2
#define BLACK 		0
#define RED		1
#define GREEN		2
#define YELLOW		3
#define BLUE            4
#define MAGENTA		5
#define CYAN		6
#define	WHITE		7

//int asksuit(std::string s);

void colorize(int type, int fgColor, int bgColor);
string SUIT[4]={"D","C","H","S"};
Card::Card(int f,Suit s)
{
  face=f;
  suit=s;
  //do we consider input error?
}

Card::Card(string s)
{
  int denom = s.find_last_of("DHCSdhcs");
  cout << "Number: "<< s.substr(0,denom);
	cout << "Suit: "<<s.substr(denom);

  string number = s.substr(0,denom);
  string color = s.substr(denom);

  if(denom==-1){
    //return error
  }

  face = atoi(number.c_str());
  suit = suitDetermine(color);
}



Card::Card(int f,string s)
{
  face=f;
  Suit t = SUIT_MAX;
  if((s.compare("Diamonds"))== 0) t = DIAMONDS;//compare in string
	else if((s.compare("Clubs"))== 0) t = CLUBS;
	else if((s.compare("Hearts"))== 0) t = HEARTS;
	else if((s.compare("Spades")) == 0) t = SPADES;
  // 	cout<<"suit in construct"<<t<<' ';
 	suit = t;
}
 
Card:: Card(const Card& other)
{
  face=other.getFace();
  suit=other.getSuit();//suit is string
  //  cout << "face: " << face << std::endl;
  // cout << "suit: " << suit << std::endl;
}

Card:: Card(int f,int s)
{
  face=f;
  // cout<<"s is "<<s<<std::endl;
  //cout<<"SUIT[s] is"<<SUIT[s]<<std::endl;
  string tempStr=SUIT[s];
  suit=suitDetermine(tempStr);

}

//int Card:: askSuitInt()
//{
//  return asksuit(this->suit);

//}

Suit Card::suitDetermine(string s){
  Suit t;
  if((s.compare("D"))== 0 || s.compare("d")==0) t = DIAMONDS;//compare in string
  else if((s.compare("C"))== 0 || s.compare("c")==0) t = CLUBS;
  else if((s.compare("H"))== 0 || s.compare("h")==0) t = HEARTS;
  else if((s.compare("S")) == 0 || s.compare("s")==0) t = SPADES;
  return t;
}


/*
const Card& Card::operator=(const Card &rhs)
{
  if(this != &rhs)
    {
      //delete old card?
      face=rhs.face;
      suit=rhs.suit;
    }
  return *this;
}
*/

bool Card::operator==(const Card &rhs) const
{
  return face==rhs.face && suit==rhs.suit;
}
int asksuit(std::string s)
{
  enum Suits{DIAMONDS,CLUBS,HEARTS,SPADES};
  if(s=="Diamonds")
    {return DIAMONDS;}
  else if(s=="Clubs")
    {return CLUBS;}
  else if(s=="Hearts")
    {return HEARTS;}
  else if(s=="Spades")
    {return SPADES;}
  else
    {std::cerr<<"invalid suit"<<std::endl;
      return -1;
    }

}

bool Card::operator<(const Card &rhs) const
{
  if(face!=rhs.face){
    return face<rhs.face;
  }// if face differ
  
  else// if face same
    {
      int suitl,suitr;
      suitl=suit;
      suitr=rhs.suit;
      return suitl<suitr;

    }
  
}

bool Card::operator<=(const Card &rhs) const
{
  return *this<rhs || *this==rhs;
}

//istream & operator>>(istream &is,Card &c)
//{
 // is>>c.face>>c.suit; //?
//  return is;
//}
// THIS IS REVISION ELEVEN (11)

ostream &operator<<(ostream &os,const Card &c)
{

  if ( c.suit == DIAMONDS)   colorize(DIM, BLACK, YELLOW);
  if ( c.suit == SPADES)     colorize(DIM, RED, CYAN);
  if (c.suit == HEARTS )     colorize(BRIGHT,WHITE , RED);
  if (c.suit == CLUBS )      colorize(BRIGHT,GREEN , MAGENTA);    
   	
  os<<c.face<<SUIT[c.suit];
  colorize(RESET, WHITE, BLACK); 
  return os;
}

void colorize(int type, int fgColor, int bgColor)
{
char command[13];
sprintf(command, "%c[%d;%d;%dm", 0x1B, type, fgColor + 30, bgColor + 40);
printf("%s", command);
}




